<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加公告</el-button>
        </el-col>
      </el-row>
      <!-- 添加用户对话框弹窗 -->
      <el-dialog title="添加公告" :visible.sync="addDialogVisible" width="50%" @close="addCloseDia">
        <el-form :model="addForm" ref="addFormRef" label-width="70px">
          <el-form-item label="发布日期" prop="date">
            <el-input v-model="addForm.date" disabled></el-input>
          </el-form-item>
          <el-form-item label="发布内容" prop="neirong">
            <el-input v-model="addForm.neirong"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>
      <el-table :data="gonggaos" border stripe>
        <el-table-column label="#" type="index" ></el-table-column>
        <el-table-column label="日期" prop="date"></el-table-column>
        <el-table-column label="内容" prop="neirong"></el-table-column>
        <el-table-column label="发布者" prop="user"></el-table-column>
        <el-table-column label="操作" align="right">
          <template slot-scope="scope">
            <el-tooltip content="删除信息" placement="top" effect="light" :enterable="false">
              <el-button
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)"
                type="danger"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.currentPage"
      :page-sizes="[5, 10, 15, 30]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=queryInfo.total>
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        currentPage: 1,
        // 当前每页显示多少条数据
        pageSize: 5,
        total: 0
      },
      gonggaos: [],
      // 添加用户对话框的显示与隐藏
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        date: '',
        neirong: '',
        user: ''
      }
    }
  },
  methods: {
    async fetch() {
      const { data: res } = await this.$http.get('/gonggaos', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取公告列表失败！')
      }
      this.gonggaos = res.gonggaosxx
      // console.log(this.users)
      this.queryInfo = res.pagination
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.fetch()
    },
    async handleCurrentChange(val) {
      this.queryInfo.currentPage = val
      this.fetch()
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除公告信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.delete(`gonggaos${id}`).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetch()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addUser() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        this.addForm.date = new Date()
        this.addForm.user = sessionStorage.getItem('name')
        const { data: res } = await this.$http.post('/gonggaos', this.addForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加公告信息失败')
        }
        this.$message.success('添加公告信息成功')
        this.addDialogVisible = false
        this.fetch()
      })
    },
    addCloseDia() {
      this.$refs.addFormRef.resetFields()
    },
    editCloseDia() {
      this.$refs.editFormRef.resetFields()
    }
  },
  created() {
    this.fetch()
  }
}
</script>

<style lang="less" scoped>
</style>
